package com.tmt.im.service.websocket;

import jakarta.websocket.HandshakeResponse;
import jakarta.websocket.server.HandshakeRequest;
import jakarta.websocket.server.ServerEndpointConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;

@Slf4j
public class WebsocketConfig extends ServerEndpointConfig.Configurator {
    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        super.modifyHandshake(sec, request, response);
        //如果连接指定了语言参数，则将语言参数保存到userProperties对象中
        Map<String, Object> userProperties = sec.getUserProperties();
        List<String> languages = request.getHeaders().get("accept-language");
        if (!CollectionUtils.isEmpty(languages)) {
            String locale = languages.get(0).split(",")[0];
            userProperties.put("locale", locale);
        }
    }

    @Override
    public <T> T getEndpointInstance(Class<T> clazz) throws InstantiationException {
        return super.getEndpointInstance(clazz);
    }
}
